package com.googlecode.scup.grammar.grammarspart;

import java.util.List;

public class Item {
    private List<Symbol> production = null;
    private int point;

    //first constructor
    public Item(Production prod) {
        this.production = prod.getProduction();
        point = 0;
    }
    //second constructor
    public Item(Production prod, int point){
        this(prod);
        this.point = point;
    }
    //third constructor
    private Item(List<Symbol> production, int point) {
        this.production = production;
        this.point = point;
    }

    public List<Symbol> getProduction() {
        return production;
    }

    public int size() {
        return production.size();
    }

    public Symbol getSymbol(int index) {
        if (index >= production.size()) return null;
        return production.get(index);
    }

    public Symbol getPointSymbol() {
        if (point >= production.size()) {
            return null;
        }
        return production.get(point);
    }

    public Item getNextProduction() {
        if (point >= production.size()) {
            return null;
        }
        return new Item(production, point + 1);
    }

    public String toString() {
        StringBuilder result = new StringBuilder();
        //T_T it's not my fall :"(
        for(int i = 0; i < production.size(); ++i) {
            if (i == point) {
                result.append(" .");
                result.append(production.get(i).getName());
            } else {
                result.append(" ");
                result.append(production.get(i).getName());
            }
        }
        return result.toString();
    }

    public boolean equals(Object obj) {
        if (! (obj instanceof Item)) {
			return false;
		}
		Item item = (Item) obj;
		if ((this.point != item.point) || (this.production.size() != item.size())) return false;
        for (int i = 0; i < this.production.size(); ++i) {
            if (! this.production.get(i).equals(item.production.get(i))) return false;
        }
        return true;
    }
}
